package middol.utils

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.micronaut.http.annotation.Body
import io.micronaut.http.annotation.Delete
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Post
import io.micronaut.http.annotation.Put
import middol.result.ResultUtils
import middolmeilisearch.DocumentsHandler
import middolmeilisearch.IndexesHandler
import middolmeilisearch.SearchHandler
import middolmeilisearch.SearchQuery
import middolmeilisearch.UpdatesHandler
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

/**
* @Description:    meilisearch utils
* @Author:         zhangjiayu@middol.com
* @CreateDate:     2020/10/31 18:46
* @Version:        1.0
*/
@CompileStatic
@Slf4j
@Component
class MeiliSearchUtils {
    @Autowired
    IndexesHandler indexesHandler
    @Autowired
    DocumentsHandler documentsHandler
    @Autowired
    UpdatesHandler updatesHandler
    @Autowired
    SearchHandler searchHandler

    /**
     * 获取所有indexes
     * @return
     */
    def getAllIndexes(){
        return indexesHandler.getAll()
    }
    /**
     * 创建 index
     * @param record
     * @return
     */
    def createIndex(Map record){
        return indexesHandler.create(record)
    }
    /**
     * 获取index
     * @param uid
     * @return
     */
    def getIndex(String uid){
        return indexesHandler.get(uid)
    }
    /**
     * 更新 index primary key
     * @param uid
     * @param record
     * @return
     */
    def updateIndexPrimaryKey(String uid, Map record){
        return indexesHandler.updatePrimaryKey(uid, record)
    }
    /**
     * 删除 index
     * @param uid
     * @return
     */
    def deleteIndex(String uid){
        indexesHandler.delete(uid)
        return ResultUtils.ok()
    }
    /**
     * 获取单个document
     * @param uid
     * @param identifier
     * @return
     */
    def getDocument(String uid, String identifier){
        return documentsHandler.getDocument(uid, identifier)
    }
    /**
     * 获取所有document
     * @param uid
     * @return
     */
    def getDocuments(String uid){
        return documentsHandler.getDocuments(uid)
    }
    /**
     * 获取所有document 分页
     * @param uid
     * @param limit
     * @return
     */
    def getDocumentsLimit(String uid, int limit){
        return documentsHandler.getDocuments(uid, limit)
    }
    /**
     * 批量添加 document
     * @param uid
     * @param document
     * @return
     */
    def addDocuments(String uid, List document){
        return documentsHandler.addDocuments(uid, document)
    }
    /**
     * 删除单个document
     * @param uid
     * @param identifier
     * @return
     */
    def deleteDocument(String uid, String identifier){
        return documentsHandler.deleteDocument(uid, identifier)
    }
    /**
     * 删除所有document
     * @param uid
     * @return
     */
    def deleteDocuments(String uid){
        return documentsHandler.deleteDocuments(uid)
    }
    /**
     * 搜索
     * @param uid
     * @param searchQuery
     * @return
     */
    def search(String uid, SearchQuery searchQuery){
        return searchHandler.search(uid, searchQuery.getQuery())
    }

    def getUpdate(String uid, int updateId) {
        return updatesHandler.getUpdate(uid, updateId)
    }

    def getUpdates(String uid){
        return updatesHandler.getUpdates(uid)
    }
}
